class PartStockinsController < ApplicationController
  before_action :set_menu

  def index
    @submenu = :part_stockin
    @title = '配件入库单'
    @part_stockins = PartStockin.all
    if params.has_key? :approve
      @part_stockin = PartStockin.find params[:approve]
      @part_stockin.update(status: 1)
      @part_stockin.add_inventory
    end
  end

  def new
    @part_stockin = PartStockin.new
  end

  def create
    @part_stockin = PartStockin.new post_params
    if @part_stockin.save
      redirect_to part_stockin_path(:id => @part_stockin.id)
    else
      render :action => 'new'
    end
  end

  def show
    @title = "配件入库单/明细"
    @buttons.push :action => part_stockins_path, :label => "返回"
    @part_stockin = PartStockin.find params[:id]
  end

  def destroy
    @part_stockin = PartStockin.find params[:id]
    @part_stockin.destroy
    redirect_to part_stockins_path
  end


  def post_params
    params.require(:part_stockin).permit(*%w(user_id note))
  end

  private
  def set_menu
    @mainmenu = :part
  end
end
